import React, { useState, useEffect } from "react";
import { Modal, message, Input } from "antd";

const demo =
  "收货人: 王世杰[5324],联系电话:17895952943-5324,收货地址:天津静海区沿庄镇双潭路静海区沿庄镇王匡村民委员会西80米[5324]";
("收货人:张鹏,地址：辽宁沈阳市浑南区五三乡凤翔大街凤祥新城西区凤祥新城2期4号楼8单元5楼1，固定电话:17897466023-3022，手机号:17897466023-3022");

const Objin = {
  收货人: "user_name",
  地址: "address",
  手机号: "telephone",
  收货地址: "address",
  联系电话: "telephone",
};

function parseTextToObject(text) {
  const keyValuePairs = text.split(/[，,]/);

  const result = {};

  keyValuePairs.forEach((pair) => {
    const [key, value] = pair.split(/[:：]/);
    let ckey = key?.trim() ?? "";
    if (key && value && Objin[ckey]) {
      result[Objin[ckey]] = value.trim();
    }
  });

  return result;
}

function SearchText({ doadd }) {
  const [value, setvalue] = useState(null);

  useEffect(() => {
    if (!value) return;
    // 匹配正则表达式，提取信息
    let matches = parseTextToObject(value);
    console.log(matches);

    const result = { ...matches };

    if (matches?.user_name && matches?.address) {
      doadd(result);
    }
  }, [value]);

  return (
    <Input
      value={value}
      onChange={(e) => {
        setvalue(e.target.value);
      }}
      allowClear
      style={{ width: "80vw", maxWidth: "200px", marginLeft: 12 }}
      placeholder="请粘贴需要识别的文本"
    />
  );
}

export default SearchText;
